package cn.tedu.blackhorse.service.impl;


import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.JobMapper;
import cn.tedu.blackhorse.mapper.SalaryMapper;
import cn.tedu.blackhorse.mapper.TeacherMapper;
import cn.tedu.blackhorse.pojo.dto.StudentAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.StudentUpdateDTO;
import cn.tedu.blackhorse.pojo.dto.TeacherAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.TeacherUpdateDTO;
import cn.tedu.blackhorse.pojo.entity.Salary;
import cn.tedu.blackhorse.pojo.entity.Teacher;
import cn.tedu.blackhorse.pojo.vo.TeacherListItemVO;
import cn.tedu.blackhorse.pojo.vo.TeacherStandardVO;
import cn.tedu.blackhorse.service.IStudentService;
import cn.tedu.blackhorse.service.ITeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理品牌业务的实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Service
public class TeacherServiceImpl implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private SalaryMapper salaryMapper;



    public TeacherServiceImpl() {
        log.info("创建业务对象：BrandServiceImpl");
    }


    @Override
    public void addNew(TeacherAddNewDTO teacherAddNewDTO) {
        int count = teacherMapper.countByJobId(teacherAddNewDTO.getJobId());
        if (count != 0){
            String message = "添加失败,职位号已被使用";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherAddNewDTO,teacher);
        int row = teacherMapper.insert(teacher);
        if (row != 1){
            String message = "添加失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        Salary salary = new Salary();
        salary.setSalary(teacherAddNewDTO.getSalary());
        salary.setTeacherId(teacherAddNewDTO.getId());
        salary.setJobId(teacherAddNewDTO.getJobId());
        int salaryRow = salaryMapper.insert(salary);
        if (salaryRow != 1){
            String message = "添加失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

    }

    @Override
    public List<TeacherListItemVO> list() {
        return teacherMapper.list();
    }

    @Override
    public void deleteById(Long id) {
        TeacherStandardVO teacher = teacherMapper.getById(id);
        if (teacher == null){
            String message = "删除失败,职位号已被使用";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row = teacherMapper.deleteById(id);
        if(row != 1){
            String message = "删除失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        int i = salaryMapper.deleteByTeacherId(teacher.getId());
        if(i != 1){
            String message = "删除失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void updateById(Long id, TeacherUpdateDTO teacherUpdateDTO) {
        TeacherStandardVO teacherStandardVO = teacherMapper.getById(id);
        if (teacherStandardVO == null){
            String message = "修改失败,所修改的老师不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherUpdateDTO,teacher);
        teacher.setId(id);
        int row = teacherMapper.updateById(teacher);
        if (row != 1){
            String message = "修改失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        if (teacherUpdateDTO.getSalary() != null && teacherUpdateDTO.getSalary().equals("")){
            Salary salary = new Salary();
            salary.setSalary(teacherUpdateDTO.getSalary());
            salary.setTeacherId(id);
            int row2 = salaryMapper.updateByTeacherId(salary);
            if (row2 != 1){
                String message = "修改失败,系统繁忙,请稍后重试";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_INSERT,message);
            }
        }
    }

    @Override
    public TeacherStandardVO getById(Long id) {
        TeacherStandardVO teacher = teacherMapper.getById(id);
        if (teacher == null){
            String message = "查询失败,查询的老师不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return teacher;
    }
}
